<?php
class UserController extends FrontBaseC{
    //配置信息
    protected $config = array(
        'client_id' => 2, //站点id
        'client_key' => 'Kr7Vg0Hx3HyKr7Vg0Hx3Hy2So5Zyb09BbLkFIv2So5Zyb09BbLkFIv', //站点密钥
        'key_validity' => 3000, //密文有效期，单位秒
    );

/*    public function __construct($config = array()) {
        if (is_array($config) && !empty($config)) {
            $this->config = array_merge($config, $this->config);
        }
    }*/

    public function actionIndex(){
        $this->run();
    }

    //执行
    public function run() {
        $code = $_GET['code'];
        parse_str(Encrypt::authcode($code, 'DECODE', $this->config['client_key']), $get);
        $post = Encrypt::authcode($_POST['code'], 'DECODE', $this->config['client_key']);
        if (!empty($post)) {
            $post = unserialize($post);
        }
        if (in_array($get['action'], array('demo', 'synlogin', 'synlogout', 'upapps', 'updatauser', 'deleteuser'))) {
            echo $this->$get['action']($get, $post);
            exit;
        } else {
            exit(0);
        }
    }

    //测试接口
    public function demo($get, $post) {
        return 1;
    }

    //同步登录
    public function synlogin($get, $post) {
        Yii::app()->user->login($get['uid'], '', '2592000', true);
        return 1;
    }

    //同步退出
    public function synlogout($get, $post) {
    	Yii::app()->user->logout(true);
        return 1;
    }

    //更新用户资料
    public function updatauser($get, $post) {
        $model = MembersModel::model()->findByPk($get['uid']);
        if($model){
            $model->attributes = $post;
            $model->save(false);
        }
        return 1;
    }

    //删除用户
    public function deleteuser($get, $post) {
    	//还没有此功能
        return 1;
    }

    //更新apps缓存
    public function upapps($get, $post) {
        return Fun::file('cache/apps', $post) ? 1 : 0;
    }	
}